package com.example.peibei.ui.fragment;

import android.graphics.Typeface;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dingtao.common.bean.Order;
import com.dingtao.common.bean.Square;
import com.dingtao.common.bean.SquareType;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDFragment;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.FollowSquarePresenter;
import com.example.peibei.service.presenter.SqueraTypePresenter;
import com.example.peibei.ui.activity.SquareDetailActivity;
import com.example.peibei.ui.activity.SquareFollowListActivity;
import com.example.peibei.ui.adapter.MyPagerAdapter;
import com.example.peibei.ui.adapter.SquareFollowAdapter;
import com.example.peibei.ui.adapter.SquareMyPagerAdapter;
import com.example.peibei.ui.weight.EmptyRecyclerView;
import com.flyco.tablayout.SlidingTabLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class QuanZiFragment extends WDFragment {
    @BindView(R.id.slideTablaout)
    SlidingTabLayout slideTablaout;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.rv_follow_list)
    EmptyRecyclerView rv_follow_list;
    @BindView(R.id.empty_view)
    LinearLayout empty_view;

    private List<Square.RecordsDTO> followList = new ArrayList<>();
    private SquareFollowAdapter squareFollowAdapter;
    private SPUtils spUtils;
    private String token;
    private FollowSquarePresenter followSquarePresenter;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private ArrayList<String> mTitles = new ArrayList<>();
    private SqueraTypePresenter squeraTypePresenter;

    @Override
    public String getPageName() {
        return "圈子Fragment";
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_quan_zi;
    }

    @Override
    protected void initView() {
        spUtils = new SPUtils(getActivity(), SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        rv_follow_list.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.HORIZONTAL, false));
        squareFollowAdapter = new SquareFollowAdapter(followList);
        rv_follow_list.setAdapter(squareFollowAdapter);
        rv_follow_list.setEmptyView(empty_view);
        followSquarePresenter = new FollowSquarePresenter(new FollowSquareCall());
        squeraTypePresenter = new SqueraTypePresenter(new SquareTypeCall());
        squeraTypePresenter.reqeust();
        squareFollowAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                List<Square.RecordsDTO> data = (List<Square.RecordsDTO>) adapter.getData();
                Square.RecordsDTO recordsDTO = data.get(position);
                Bundle bundle = new Bundle();
                bundle.putString("imgUrl", recordsDTO.getHeaderUrl());
                bundle.putString("backUrl", recordsDTO.getBackUrl());
                bundle.putString("circleName", recordsDTO.getCircleName());
                bundle.putString("circleTypeId", recordsDTO.getCircleTypeId());
                bundle.putString("id", recordsDTO.getId() + "");
                bundle.putString("personNum", recordsDTO.getPersonNum());
                bundle.putBoolean("follow", true);
                intent(SquareDetailActivity.class, bundle);
            }
        });

        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                int tabCount = slideTablaout.getTabCount();
                for (int i = 0; i < tabCount; i++) {
                    TextView title = slideTablaout.getTitleView(i);
                    if (i == position) {
                        title.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                        title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
                    } else {
                        title.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                        title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @OnClick({R.id.iv_more,R.id.tv_more})
    public void more() {
        intent(SquareFollowListActivity.class);
    }

    @Override
    public void onResume() {
        super.onResume();
        followSquarePresenter.reqeust(true, "Bearer " + token);
    }

    public void resumeFollowList(){
        followSquarePresenter.reqeust(true, "Bearer " + token);
    }

    class FollowSquareCall implements DataCall<Square> {
        @Override
        public void success(Square data, Object... args) {
            //添加列表并刷新
            if (followSquarePresenter.getPage() == 1) {
                followList.clear();
            }
            squareFollowAdapter.setNewInstance(data.getRecords());
            squareFollowAdapter.notifyDataSetChanged();
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }


    class SquareTypeCall implements DataCall<List<SquareType>> {
        @Override
        public void success(List<SquareType> data, Object... args) {
            if (data != null && data.size() > 0) {
                for (int i = 0; i < data.size(); i++) {
                    mTitles.add(data.get(i).getTypeName());
                    fragments.add(new SquareListFragment(String.valueOf(data.get(i).getId())));
                }
                SquareMyPagerAdapter adapter = new SquareMyPagerAdapter(getChildFragmentManager(), fragments, mTitles);
                viewpager.setAdapter(adapter);
                viewpager.setOffscreenPageLimit(2);
                slideTablaout.setViewPager(viewpager);
                viewpager.setCurrentItem(0);
            }
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }
}
